#include "VzlprWrap.h"

NAN_METHOD(on) {
  VERIFY_INIT();
  VERIFY_ON_ARGS();

  Nan::Utf8String p0(info[0]);
  v8::Local<v8::Function> p1 = v8::Local<v8::Function>::Cast(info[1]);
  std::string event = std::string(*p0);
  Nan::Callback *callback = new Nan::Callback(p1);

  g_manager->on(event, callback);

  info.GetReturnValue().SetUndefined();
}

NAN_METHOD(setup) {
  int ret = VzLPRClient_Setup();
  if (ret == 0) {
    SAFE_DELETE(g_manager);
    g_manager = new DeviceManager;
  }
  info.GetReturnValue().Set(ret);
}

NAN_METHOD(cleanup) {
  VzLPRClient_Cleanup();
  SAFE_DELETE(g_manager);
}

NAN_METHOD(startFindDevice) {
  RuleObject::Ptr rule;
  ASYNC_INVOKE_DEVICE_MANAGER(BIND_NO_PARAM_WITH_RESULT, startFindDevice);
}

NAN_METHOD(stopFindDevice) {
  RuleObject::Ptr rule;
  ASYNC_INVOKE_DEVICE_MANAGER(BIND_NO_PARAM_NO_RESULT, stopFindDevice);
}

NAN_METHOD(open) {
  RuleObject::Ptr rule = RuleObject::CreateObjectRule();
  rule->AddBasicRule("ip", StringType, true);
  rule->AddBasicRule("port", UintType, true);
  rule->AddBasicRule("username", StringType, true);
  rule->AddBasicRule("password", StringType, true);
  ASYNC_INVOKE_DEVICE_MANAGER(BIND_WITH_PARAM_WITH_RESULT, open);
}

NAN_METHOD(close) {
  RuleObject::Ptr rule = RuleObject::CreateBasicRule(DeviceType);
  ASYNC_INVOKE_DEVICE_MANAGER(BIND_WITH_PARAM_WITH_RESULT, close);
}

NAN_METHOD(getUpdateFileVersion) {
  RuleObject::Ptr rule = RuleObject::CreateBasicRule(StringType);
  ASYNC_INVOKE_DEVICE_MANAGER(BIND_WITH_PARAM_WITH_RESULT, getUpdateFileVersion);
}